
from exts import mysql

import re
# 搜索特价
def searchche(query,day):
    cursor=mysql.cursor()
    cursor.execute("SELECT * FROM `hotelinfo` WHERE hotelinfo.location LIKE '"+query+"%'")
    t=cursor.fetchall()
    cursor.execute("SELECT * FROM `rooms` WHERE rooms.hotel_num in (SELECT hotelinfo.hid FROM `hotelinfo` WHERE hotelinfo.location LIKE '"+query+"%')")
    troom=cursor.fetchall()
    result = []

    for i in range(len(t)):
        if t[i][7]!='null':
            resultdict = dict(hid=t[i][0], hname=t[i][2], point=float(t[i][7]), star=t[i][9], suslevel=t[i][10],htype=t[i][3],dis=int(t[i][14]))
            rooms=[]
            for item in troom:
                if item[0]==t[i][0]:
                    rooms.append(item)
            pricelist=[]
            for j in range(len(rooms)):
                price=rooms[j][10]
                number = re.findall("\d+", str(price))
                lens=len(number)
                jie=0
                numberprice=0
                for x in range(lens-1,-1,-1):
                    #print(int(number[x]))
                    numberprice+=int(number[x])*(1000**jie)
                    jie+=1
                if numberprice!=0:
                    pricelist.append(numberprice)#所有最小
            if pricelist:
                minp=min(pricelist)
                index=pricelist.index(minp)
                resultdict['room_name'] = rooms[index][3]
                resultdict['bed_type'] = rooms[index][5]
                resultdict['rooms_price_other'] = rooms[index][11]
                resultdict['room_price'] =minp*day
                resultdict['room_cancel']=rooms[index][13]
                resultdict['rooms_advance_pay']=rooms[index][14]
                resultdict['room_breakfast']=rooms[index][12]
                result.append(resultdict)
    #print(result)
    return result

def shaixuan(temp):
    settype=set()
    for i in range(len(temp)):
        #print(temp[i])
        settype.add(temp[i]['htype'])
    typelist=list(settype)
    numberlist=[]
    for i in range(len(typelist)):
        numberlist.append(0)
    for i in range(len(temp)):
        for j in range(len(typelist)):
            if temp[i]['htype']==typelist[j]:
                numberlist[j]+=1
    typesearch = dict(zip(typelist, numberlist))
    print(typesearch)
    return typesearch


#确定品牌类型及其数量，设施同理
def brandandfac(query):#根据city
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `hoteldescribes` WHERE hoteldescribes.hid in (SELECT hotelinfo.hid FROM hotelinfo WHERE hotelinfo.location LIKE '"+query+"%')")
    t = cursor.fetchall()
    brandset=set()
    facset=set()
    brandtemp=[]
    for i in t:
        brandstr=i[6]
        brand=brandstr.split(':')[-1]
        branddic = dict(hid=i[0],brand=brand)
        brandset.add(brand)
        facstr=i[7]
        branddic['fac']=facstr
        brandtemp.append(branddic)
        fac=facstr.split('+')
        for item in fac:
            facset.add(item.strip())
    faclist=list(facset)
    brandlist=list(brandset)
    bnumberlist = []
    fnumberlist = []
    for i in range(len(brandlist)):
        bnumberlist.append(0)
    for i in range(len(faclist)):
        fnumberlist.append(0)
    for i in range(len(t)):
        for j in range(len(brandlist)):
            tempbrand=t[i][6].split(':')[-1]
            if tempbrand == brandlist[j]:
                bnumberlist[j] += 1
        for k in range(len(faclist)):
            tempfacstr = t[i][7]
            fac = tempfacstr.split('+')
            for count in range(len(fac)):
                fac[count]=fac[count].strip()
            if faclist[k] in fac:
                fnumberlist[k]+=1

    facsearch= dict(zip(faclist, fnumberlist))
    brandsearch = dict(zip(brandlist, bnumberlist))
    #print(brandtemp)
    return brandtemp,facsearch,brandsearch

def checktype(typelist,brandlist,faclist,starlist,pointlist,cancellist,temp,brandtemp):

    if brandlist:
        result = []
        hid=[]
        for j in brandtemp:
            if j['brand'] in brandlist:
                hid.append(j['hid'])
        for i in temp:
            if i['hid'] in hid:
                result.append(i)
        temp=result
    if faclist:
        result=[]
        hid=[]
        for j in brandtemp:
            for item in faclist:
                r1=re.search(item,j['fac'])
                if r1:
                    hid.append(j['hid'])
                    break
        for i in temp:
            if i['hid'] in hid:
                result.append(i)
        temp=result

    if typelist:
        result = []
        for j in temp:
            if (j['htype'] in typelist):
                result.append(j)
        temp=result

    if starlist:
        result = []
        for j in temp:
            if (j['star'] in starlist):
                result.append(j)
        temp = result

    if pointlist:
        result = []
        for j in temp:
            if j['point']!='null'and (float(j['point'])>=min(pointlist)) :
                result.append(j)
        temp=result

    if cancellist:
        result=[]
        for j in temp:
            for item in cancellist:
                r2=re.search(item,j['room_cancel'])
                if r2:
                    result.append(j)
                break

    #result.clear()
    #print(result)
    return result

def sortprice(r):
    r.sort(key=lambda s: s["room_price"])
    return r

def sortsus(r):
    r.sort(key=lambda s: s["suslevel"], reverse = True)
    return r

def sortdis(r):
    r.sort(key=lambda s: s["dis"])
    return r

def sortp(r):
    r.sort(key=lambda s: s["point"], reverse = True)
    return r

def pointlist(r):
    l1=[x for x in r if x['point'] >= 9]
    l2 = [x for x in r if x['point'] >= 8]
    l3 = [x for x in r if x['point'] >= 7]
    l4 = [x for x in r if x['point'] >= 6]
    t1=[9,8,7,6]
    t2=[len(l1),len(l2),len(l3),len(l4)]
    result=dict(zip(t1,t2))
    return result

def starlist(r):
    l1=[x for x in r if x['star'] ==2]
    l2 = [x for x in r if x['star'] == 3]
    l3 = [x for x in r if x['star'] == 4]
    l4 = [x for x in r if x['star'] == 5]
    l5 = [x for x in r if x['star'] == 0]
    t1=[2,3,4,5,0]
    t2=[len(l1),len(l2),len(l3),len(l4),len(l5)]
    result=dict(zip(t1,t2))
    return result

def dislist(r):
    l1=[x for x in r if x['dis'] <=10]
    l2 = [x for x in r if x['dis'] <= 30]
    l3 = [x for x in r if x['dis'] <= 50]
    t1=[1,3,5]
    t2=[len(l1),len(l2),len(l3)]
    result=dict(zip(t1,t2))
    return result


def fliter(typelist,pointlist,dislist,temp):
    result=temp
    if typelist:
        result = []
        for j in temp:
            if (j['htype'] in typelist):
                result.append(j)
        temp=result

    if dislist:
        result = []
        for j in temp:
            if j['dis'] != 'null' and (float(j['dis'])/10 <= min(dislist)):
                result.append(j)
        temp = result

    if pointlist:
        result = []
        for j in temp:
            if j['point']!='null'and (float(j['point'])>=min(pointlist)) :
                result.append(j)
    #print(result)
    return result

if __name__ == '__main__':

    a=searchche('北京',1)
    #b,c,d=brandandfac('北京')
    #checktype(typelist=['酒店'],brandlist=[],faclist=['停车场','餐厅','客房服务'],pointlist=[9],starlist=[5],cancellist=['免费取消'],temp=a,brandtemp=b)
    #brandandfac('北京')
    #a=searchche('昆明')
    #b=sortp(a)
    #print(a)
    #print("排序结果")
    #print(b)
    #print(a)
    print(sortsus(a))
   # b=fliter(typelist=['公寓'],pointlist=[],dislist=[],temp=a)
   # print(b)
    #b=sortprice(a)
    #print(b)